// pages/showWeek/showWeek.js
import nami from '../../nami/index';
import utils from '../../utils/utils';

let chooseYear = null;
let chooseMonth = null;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    hasEmptyGrid: false,
    showPicker: false,
    char_lt: '<',
    char_rt: '>',
    diary: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    const date = new Date();
    const curYear = date.getFullYear();
    const curMonth = date.getMonth() + 1;
    that.setData({
      curYear,
      curMonth,
    });
    nami.request({
      url: '/request/diary/showWeek.groovy',
      data: {
        month: that.data.curMonth,
        year: that.data.curYear
      },
      success: function (res) {
        console.log(res.data);
        if (res.data.length != 0) {
            for (var x in res.data) {
              let date = new Date(res.data[x].DATE);
              res.data[x].next = utils.addDate(date,6);
            }
            that.setData({
              diary: res.data
            });
        } else {
          wx.showModal({
            title: '提示',
            content: '当月没有周记哦，请重新选择日期',
            showCancel: false,
            success: function(resp) {
              if (resp.confirm) {
                console.log('用户点击确定')
              }
            }
          });
        }
      }
    });
  },

  handleCalendar(e) {
    var that = this;
    const handle = e.currentTarget.dataset.handle;
    const curYear = that.data.curYear;
    const curMonth = that.data.curMonth;
    if (handle === 'prev') {
      let newMonth = curMonth - 1;
      let newYear = curYear;
      if (newMonth < 1) {
        newYear = curYear - 1;
        newMonth = 12;
      }
      that.setData({
        curYear: newYear,
        curMonth: newMonth
      });
    } else {
      let newMonth = curMonth + 1;
      let newYear = curYear;
      if (newMonth > 12) {
        newYear = curYear + 1;
        newMonth = 1;
      }
      that.setData({
        curYear: newYear,
        curMonth: newMonth
      });
    }

    nami.request({
      url: '/request/diary/showWeek.groovy',
      data: {
        month: that.data.curMonth,
        year: that.data.curYear
      },
      success: function (res) {
        console.log(res.data);
        if (res.data.length != 0) {
          for (var x in res.data) {
            let date = new Date(res.data[x].DATE);
            res.data[x].next = utils.addDate(date, 6);
          }
            that.setData({
              diary: res.data
            });
        } else {
          wx.showModal({
            title: '提示',
            content: '当月没有周记哦，请重新选择日期',
            showCancel: false,
            success: function(resp) {
              if (resp.confirm) {
                console.log('用户点击确定')
              }
            }
          });
        }
      }
    });
  },

  chooseYearAndMonth() {
    var that = this;
    const curYear = that.data.curYear;
    const curMonth = that.data.curMonth;
    let pickerYear = [];
    let pickerMonth = [];
    for (let i = 1900; i <= 2100; i++) {
      pickerYear.push(i);
    }
    for (let i = 1; i <= 12; i++) {
      pickerMonth.push(i);
    }
    const idxYear = pickerYear.indexOf(curYear);
    const idxMonth = pickerMonth.indexOf(curMonth);
    that.setData({
      pickerValue: [ idxYear, idxMonth ],
      pickerYear,
      pickerMonth,
      showPicker: true,
    });
  },
  pickerChange(e) {
    var that = this;
    const val = e.detail.value;
    chooseYear = that.data.pickerYear[val[0]];
    chooseMonth = that.data.pickerMonth[val[1]];
  },
  tapPickerBtn(e) {
    var that = this;
    const type = e.currentTarget.dataset.type;
    const o = {
      showPicker: false,
    };
    if (type === 'confirm') {
      o.curYear = chooseYear;
      o.curMonth = chooseMonth;
    }

    that.setData(o);

    nami.request({
      url: '/request/diary/showWeek.groovy',
      data: {
        month: that.data.curMonth,
        year: that.data.curYear
      },
      success: function (res) {
        console.log(res.data);
        if (res.data.length != 0) {
            that.setData({
              diary: res.data
            });
        } else {
          wx.showModal({
            title: '提示',
            content: '当月没有周记哦，请重新选择日期',
            showCancel: false,
            success: function(resp) {
              if (resp.confirm) {
                console.log('用户点击确定')
              }
            }
          });
        }
      }
    });
  },

})